$(function(){
	var codeTimer = null;
	// 获取验证吗
	$("#get_vcode").on("click", function(e){
		e.stopPropagation();
		var that = $(this);
		var phone = $("#phone").val().trim();
		if(phone === ""){
			alert("手机号码不能为空");
		}else if(!checkPhone(phone)){
			alert("手机号码格式不正确");
		}else{
			var option = {};
			option.phone = phone;
			option.operation = 0;
			option.success = function(res){
				var codeTime = 60;
				that.attr("disabled", true);
                codeTimer = setInterval(function () {
                  if (codeTime >= 0) {
                    that.text(codeTime);
                    --codeTime
                  } else {
                    clearInterval(codeTimer);
                    that.text("获取验证吗");
					that.removeAttr("disabled");
                  }
                }, 1000)
			}
			option.fail = function(res){
				alert("获取验证码失败，请重试");
			}
			getVerifyCode(option)
		}
	})
	// 点击提交注册
	$("#submit_btn").on("click", function(e){
		e.stopPropagation();
		var that = $(this);
		var phone = $("#phone").val().trim();
		var v_code = $("#v_code").val().trim();
		var invite_code = $("#invite_code").val().trim();
		var password = $("#password").val().trim();
		var sure_password = $("#sure_password").val().trim();
		var user_name = $("#user_name").val().trim();
		if(phone === ""){
			alert("手机号码不能为空");
		}else if(!checkPhone(phone)){
			alert("手机号码格式不正确");
		}else if(v_code === ""){
			alert("验证码不能为空");
		}else if(invite_code === ""){
			alert("邀请码不能为空");
		}else if(password === ""){
			alert("密码不能为空");
		}else if(sure_password !== password){
			alert("前后两次密码输入不一致");
		}else{
			$.post("/api/auth/signup/", {
				phone: phone,
				v_code: v_code,
				invite_code: invite_code,
				password: password,
				user_name: user_name
			}, function(res){
				if(res.code === 0){
					window.location.href = 'index.html';
				}else{
					alert(res.msg)
				}
			}).error(function(res){
				var res = JSON.parse(res.responseText)
				alert(res.msg)
			})
		}

	})
})